package cn.iocoder.yudao.module.promotion.controller.app.rankinglist;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.promotion.controller.app.rankinglist.vo.AppRankingListDetailRespVo;
import cn.iocoder.yudao.module.promotion.controller.app.rankinglist.vo.AppRankingListRespVO;
import cn.iocoder.yudao.module.promotion.controller.app.rankinglist.vo.AppRankingListUserInfoRespVo;
import cn.iocoder.yudao.module.promotion.dal.dataobject.rankinglist.RankingListDO;
import cn.iocoder.yudao.module.promotion.service.rankinglist.RankingListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "App - 活动排行榜")
@RestController
@RequestMapping("/promotion/ranking-list")
@Validated
public class AppRankingListController {

    @Resource
    private RankingListService rankingListService;

//    @PostMapping("/create")
//    @Operation(summary = "创建活动排行榜")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:create')")
//    public CommonResult<Long> createRankingList(@Valid @RequestBody AppRankingListSaveReqVO createReqVO) {
//        return success(rankingListService.createRankingList(createReqVO));
//    }
//
//    @PutMapping("/update")
//    @Operation(summary = "更新活动排行榜")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:update')")
//    public CommonResult<Boolean> updateRankingList(@Valid @RequestBody AppRankingListRespVO updateReqVO) {
//        rankingListService.updateRankingList(updateReqVO);
//        return success(true);
//    }
//
//    @DeleteMapping("/delete")
//    @Operation(summary = "删除活动排行榜")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:delete')")
//    public CommonResult<Boolean> deleteRankingList(@RequestParam("id") Long id) {
//        rankingListService.deleteRankingList(id);
//        return success(true);
//    }

//    @GetMapping("/get")
//    @Operation(summary = "获得活动排行榜")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:query')")
//    public CommonResult<AppRankingListRespVO> getRankingList(@RequestParam("id") Long id) {
//        RankingListDO rankingList = rankingListService.getRankingList(id);
//        return success(BeanUtils.toBean(rankingList, AppRankingListRespVO.class));
//    }

    @GetMapping("/now")
    @Operation(summary = "获得当前进行中的活动排行榜")
    public CommonResult<AppRankingListRespVO> getRankingListNow() {
        LocalDateTime now = LocalDateTime.now();
        RankingListDO rankingList = rankingListService.getRankingListNow(now);
        return success(BeanUtils.toBean(rankingList, AppRankingListRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得活动排行榜分页")
//    @PreAuthorize("@ss.hasPermission('promotion:ranking-list:query')")
//    public CommonResult<PageResult<AppRankingListRespVO>> getRankingListPage(@Valid AppRankingListPageReqVO pageReqVO) {
//        PageResult<RankingListDO> pageResult = rankingListService.getRankingListPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, AppRankingListRespVO.class));
//    }

     //getRankingListDetailById
    @GetMapping("/get")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "根据id获取排行榜活动详情")
    public CommonResult<AppRankingListDetailRespVo> getRankingListDetailById(@RequestParam("id") Long id) {

        return success(rankingListService.getAppRankingListDetailById(id));
    }


    @GetMapping("/my_score")
    @Operation(summary = "获得当前用户排行榜活动积分")
    @PreAuthenticated
    public CommonResult<AppRankingListUserInfoRespVo> getMyRankingListDetailById(@RequestParam("id") Long id) {
        Long loginUserId = getLoginUserId();
        AppRankingListUserInfoRespVo appRankingListUserInfoRespVo = rankingListService.getMyRankingListDetailById(id,loginUserId);
        return success(appRankingListUserInfoRespVo);
    }



}